package com.hmkj.Utils.mail;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MailSender {
	private MailAuthenticator authenticator;
	private static final Logger log = LoggerFactory.getLogger(MailSender.class);

	/**
	 * 以文本格式发送邮件
	 * 
	 * @param MailSenderInfo
	 *            待发送的邮件信息
	 * @return boolean 邮件是否发送成功
	 */
	public boolean sendTextMail(MailSenderInfo mailInfo) {
		Properties properties = mailInfo.getProperties();
		// 如果需要身份验证，则创建一个密码验证器
		if (mailInfo.isValidate()) {
			authenticator = new MailAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
		}
		// 根据邮件会话属性和密码验证器构造一个发送邮件的session
		Session sendMailSession = Session.getDefaultInstance(properties, authenticator);
		try {
			// 根据session创建一个没有附件的邮件消息
			Message mailMessage = new MimeMessage(sendMailSession);
			// 创建邮件发送者发送者地址
			Address fromAddress = new InternetAddress(mailInfo.getFromAddress());
			// 设置邮件消息的发送者
			mailMessage.setFrom(fromAddress);
			// 创建邮件的接收者地址，然后设置到邮件消息中
			Address toAddress = new InternetAddress(mailInfo.getToAddress());
			mailMessage.setRecipient(Message.RecipientType.TO, toAddress);
			// 设置邮件消息的主题
			mailMessage.setSubject(mailInfo.getSubject());
			// 设置邮件的发送时间
			mailMessage.setSentDate(new Date());
			// 设置邮件消息的主要内容
			mailMessage.setText(mailInfo.getContent());
			// 发送邮件
			Transport.send(mailMessage);
			return true;
		} catch (MessagingException e) {
			log.error(e.getMessage());
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 发送HTML格式的EMAIL
	 * 
	 * @param MailSenderInfo待发送的邮件信息
	 * @return boolean 返回邮件是否发送成功
	 */
	public boolean sendHtmlMail(MailSenderInfo mailInfo) {
		Properties properties = mailInfo.getProperties();
		// 如果需要身份验证，则创建一个密码验证器
		if (mailInfo.isValidate()) {
			authenticator = new MailAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
		}
		// 根据邮件会话属性和密码验证器构造一个发送邮件的session
		Session sendMailSession = Session.getInstance(properties, authenticator);
		try {
			// 根据session创建一个没有附件的邮件消息
			Message mailMessage = new MimeMessage(sendMailSession);
			// 创建邮件发送者发送者地址
			Address fromAddress = new InternetAddress(mailInfo.getFromAddress());
			// 设置邮件消息的发送者
			mailMessage.setFrom(fromAddress);
			// 创建邮件的接收者地址，然后设置到邮件消息中
			Address toAddress = new InternetAddress(mailInfo.getToAddress());
			mailMessage.setRecipient(Message.RecipientType.TO, toAddress);
			// 设置邮件消息的主题

			String cStr = mailInfo.getSubject();
			String cSubject = cStr;
			try {
				// cSubject = new String(cSubject.getBytes("GB2312"));
				cSubject = MimeUtility.encodeWord(cStr, "UTF-8", "Q");
			} catch (UnsupportedEncodingException e) {
				log.error(e.getMessage());
				cSubject = cStr;
			}
			mailMessage.setSubject(cSubject);
			// 设置邮件的发送时间
			mailMessage.setSentDate(new Date());
			// 创建一个MiniMultipart类型的容器类，包含MimeBodyPart类型的对象
			Multipart mainPart = new MimeMultipart();
			// 创建一个包含HTML内容的MimeBodyPart
			BodyPart html = new MimeBodyPart();
			// 设置HTML内容 格式
			html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
			mainPart.addBodyPart(html);
			// 将MiniMultipart对象设置为邮件内容
			mailMessage.setContent(mainPart);
			// 发送邮件
			Transport.send(mailMessage);
			return true;
		} catch (MessagingException e) {
			log.equals("错误SMTP：" + mailInfo.getFromAddress());
			log.equals("错误USER：" + mailInfo.getUserName());
			log.error(e.getMessage());
			e.printStackTrace();
			return false;
		}
	}
}